﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Synchronization;
using Microsoft.Synchronization.Data;

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string xmlSyncLayout = "";
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                //lbTableNames.DisplayMember = "Name";
                //lbTableNames.ValueMember = "Selected";

                DevExpress.XtraGrid.Columns.GridColumn colSelected = new DevExpress.XtraGrid.Columns.GridColumn();
                colSelected.Visible = true;
                colSelected.VisibleIndex = 0;
                colSelected.Width = 40;
                colSelected.FieldName = "Selected";
                colSelected.Name = "colSelected";
                viewTables.Columns.Add(colSelected);

                DevExpress.XtraGrid.Columns.GridColumn colName = new DevExpress.XtraGrid.Columns.GridColumn();
                colName.Visible = true;
                colName.VisibleIndex = 1;
                colName.Width = 120;
                colName.FieldName = "Name";
                colName.Name = "colName";
                viewTables.Columns.Add(colName);

                gridTables.DataSource = Aiss.Sync.Tables.GetTables();

                //lbColumns.DisplayMember = "Name";
                //lbColumns.ValueMember = "Selected";

                colSelected = new DevExpress.XtraGrid.Columns.GridColumn();
                colSelected.Visible = true;
                colSelected.VisibleIndex = 0;
                colSelected.Width = 40;
                colSelected.FieldName = "Selected";
                colSelected.Name = "colSelected";
                viewColumns.Columns.Add(colSelected);

                colName = new DevExpress.XtraGrid.Columns.GridColumn();
                colName.Visible = true;
                colName.VisibleIndex = 1;
                colName.Width = 120;
                colName.FieldName = "Name";
                colName.Name = "colName";
                viewColumns.Columns.Add(colName);

                gridColumns.DataBindings.Add("DataSource", gridTables.DataSource, "Columns");

                LoadCheckedTables();
            }
            catch (Exception ex)
            {
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string scopeName = "Areas1";
                
                Provider p = new Provider();
                p.TablesToSync = from i in ((Aiss.Sync.Tables)gridTables.DataSource) where i.Selected == true select i;

                //p.ConfigureScope("katalun4o-pc", "RetailMain", scopeName);
                p.ConfigureScope(Aiss.Sync.Settings.ConnStringMain, scopeName);

                //p.ConfigureScope("katalun4o-pc", "RetailRemote", scopeName);
                p.ConfigureScope(Aiss.Sync.Settings.ConnStringRemote, scopeName);

                //Microsoft.Synchronization.Data.SqlServer.SqlSyncProvider providerLocal = p.ConfigureSqlSyncProvider("katalun4o-pc", "RetailMain",scopeName);
                Microsoft.Synchronization.Data.SqlServer.SqlSyncProvider providerLocal = p.ConfigureSqlSyncProvider(Aiss.Sync.Settings.ConnStringMain, scopeName);

                //Microsoft.Synchronization.Data.SqlServer.SqlSyncProvider providerRemote = p.ConfigureSqlSyncProvider("katalun4o-pc", "RetailRemote", scopeName);
                Microsoft.Synchronization.Data.SqlServer.SqlSyncProvider providerRemote = p.ConfigureSqlSyncProvider(Aiss.Sync.Settings.ConnStringRemote, scopeName);

                SyncOrchestrator orchestrator = new SyncOrchestrator();
                orchestrator.LocalProvider = providerLocal;
                orchestrator.RemoteProvider = providerRemote;
                orchestrator.Direction = SyncDirectionOrder.DownloadAndUpload;
                orchestrator.Synchronize();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSync_Click(object sender, EventArgs e)
        {
            Provider p = new Provider();

            string scopeName = "Areas1";

            //Microsoft.Synchronization.Data.SqlServer.SqlSyncProvider providerLocal = p.ConfigureSqlSyncProvider("katalun4o-pc", "RetailMain", scopeName);
            //Microsoft.Synchronization.Data.SqlServer.SqlSyncProvider providerRemote = p.ConfigureSqlSyncProvider("katalun4o-pc", "RetailRemote", scopeName);

            Microsoft.Synchronization.Data.SqlServer.SqlSyncProvider providerLocal = p.ConfigureSqlSyncProvider(Aiss.Sync.Settings.ConnStringMain, scopeName);
            Microsoft.Synchronization.Data.SqlServer.SqlSyncProvider providerRemote = p.ConfigureSqlSyncProvider(Aiss.Sync.Settings.ConnStringRemote, scopeName);

            SyncOrchestrator orchestrator = new SyncOrchestrator();
            orchestrator.LocalProvider = providerLocal;
            orchestrator.RemoteProvider = providerRemote;
            orchestrator.Direction = SyncDirectionOrder.DownloadAndUpload;
            orchestrator.Synchronize();
        }

        private void lbTableNames_SelectedIndexChanged(object sender, EventArgs e)
        {
            //string currentTable = lbTableNames.SelectedItem.ToString();
            //lbColumns.DataSource = Aiss.Sync.Column.GetColumns(currentTable);
        }

        //Aiss.Sync.Tables TablesToSync;
        private void button2_Click(object sender, EventArgs e)
        {
            //choose columns
            /*foreach (string s in gridco.SelectedItems)
            {
                string tableName = lbTableNames.SelectedItem.ToString();
                Aiss.Sync.Table table = (from i in TablesToSync where i.Name == tableName select i).FirstOrDefault<Aiss.Sync.Table>();
                if (table == null)
                {
                    table = new Aiss.Sync.Table(tableName);
                    TablesToSync.Add(table);
                }
            }
            */
        }

        private void LoadCheckedTables()
        {
            Aiss.Sync.SyncInfo syncInfo = Aiss.Sync.SyncInfo.GetSyncInfo();
            if (syncInfo.ID == 0)
                return;
            string xml = syncInfo.Xml;

            System.Xml.Linq.XDocument d = System.Xml.Linq.XDocument.Parse(xml);
            var tables = d.Element("Tables").Elements("Table");
            foreach (var table in tables)
            {
                var x = (from i in ((Aiss.Sync.Tables)gridTables.DataSource) where i.Name == table.Attribute("Name").Value select i).FirstOrDefault();
                x.Selected = true;

                var columns = table.Elements("Column");
                foreach (var column in columns)
                {
                    var y = (from i in x.Columns where i.Name == column.Attribute("Name").Value select i).FirstOrDefault();
                    y.Selected = true;
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var tables = from i in ((Aiss.Sync.Tables)gridTables.DataSource) where i.Selected == true select i;
            System.Xml.Linq.XDocument d = new System.Xml.Linq.XDocument();
            d.Add(new System.Xml.Linq.XElement("Tables"));
            foreach (var table in tables)
            {
                System.Xml.Linq.XElement tableElement = new System.Xml.Linq.XElement("Table");
                System.Xml.Linq.XAttribute tableNameAttribute = new System.Xml.Linq.XAttribute("Name", table.Name);
                tableElement.Add(tableNameAttribute);

                foreach (var col in table.Columns)
                {
                    if (col.Selected == false)
                        continue;
                    System.Xml.Linq.XElement elColumn = new System.Xml.Linq.XElement("Column");
                    System.Xml.Linq.XAttribute columnNameAttribute = new System.Xml.Linq.XAttribute("Name", col.Name);
                    elColumn.Add(columnNameAttribute);
                    tableElement.Add(elColumn);
                }
                d.Element("Tables").Add(tableElement);

            }
            string xmlToSave = d.ToString();

            Aiss.Sync.SyncInfo syncInfo = Aiss.Sync.SyncInfo.GetSyncInfo();
            if (syncInfo.ID == 0)
                syncInfo = Aiss.Sync.SyncInfo.NewSyncInfo();
            syncInfo.Xml = xmlToSave;
            syncInfo.Save();
        }

    }
}
